package com.plian.system.service.szyd.check;

import com.plian.Tools.CacheUtil;
import com.plian.Tools.StringUtil;
import com.plian.system.bean.wtzg.RectificationSituationExcelBean;
import com.plian.system.excel.ImportExcelUtil;
import com.plian.system.excel.ImportResult;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AllArgsConstructor
@Slf4j
public class TbImportantMattersExcelCheckData {

	private final static String OBJECT_MSG = "未找到对应对象!";
	/**
	 * 人员档案导入 主表校验
	 * @param readExcels Excel导入的问题整改数据
	 * @param progressId 流程key
	 * @param codeMap 问题整改中所有code转id的参照字典
	 * @param nameMap 问题整改中所有name转id的参照字典(指单位)
	 * @return ImportResult
	 */
	public static ImportResult checkStaffData(List<Object> readExcels, String progressId,
											  HashMap<String, List<String>> codeMap, HashMap<String, Map<String,String>> nameMap) {
		ImportResult importResult = new ImportResult(true,"");
		StringBuilder msg = new StringBuilder();

		List<String> codeList = new ArrayList<>();

		//数据判空及数据类型校验
		for (Object readExcel : readExcels) {
			TbImportantMattersExcelBean rectificationSituationExcelBean=(TbImportantMattersExcelBean)readExcel;
			int index = readExcels.indexOf(readExcel);
			//编码判重队列
			codeList.add(rectificationSituationExcelBean.getSerialNumber());
			//判空
			msg.append(ImportExcelUtil.checkEmpty("0", "事项名称", rectificationSituationExcelBean.getItemName(), index).getMsg());
			msg.append(ImportExcelUtil.checkEmpty("0", "责任部门", rectificationSituationExcelBean.getDutyDept(), index).getMsg());
			msg.append(ImportExcelUtil.checkEmpty("0", "党委会决策时间", rectificationSituationExcelBean.getDecisionDateStr(), index).getMsg());
			msg.append(ImportExcelUtil.checkEmpty("0", "决策会议决策时间", rectificationSituationExcelBean.getDecisionMeetDateStr(), index).getMsg());
			msg.append(ImportExcelUtil.checkEmpty("0", "事项类别", rectificationSituationExcelBean.getMatterType(), index).getMsg());
			msg.append(ImportExcelUtil.checkEmpty("0", "事项内容", rectificationSituationExcelBean.getMatterContent(), index).getMsg());
//			msg.append(ImportExcelUtil.checkEmpty("0", "决策意见", rectificationSituationExcelBean.getDecisionAdvince(), index).getMsg());
			msg.append(ImportExcelUtil.checkEmpty("0", "是否党委会前置研究", rectificationSituationExcelBean.getIsFrontStr(), index).getMsg());
			msg.append(ImportExcelUtil.checkEmpty("0", "决策会议类型", rectificationSituationExcelBean.getDecisionMeetType(), index).getMsg());
//			msg.append(ImportExcelUtil.checkEmpty("0", "完成情况", rectificationSituationExcelBean.getCompletionStatus(), index).getMsg());
			msg.append(ImportExcelUtil.checkType(ImportExcelUtil.TYPE_DATE_TWO, "0", "党委会决策时间",
					rectificationSituationExcelBean.getDecisionDateStr(), index).getMsg());
			msg.append(ImportExcelUtil.checkType(ImportExcelUtil.TYPE_DATE_TWO, "0", "决策会议决策时间",
					rectificationSituationExcelBean.getDecisionMeetDateStr(), index).getMsg());

		}
		//判断完数据类型若有错直接返回
		if (msg.toString().length() > 0) {
			return new ImportResult(false, msg.toString());
		}
		//对象和字典是否存在校验
		int size=readExcels.size();
		int count=0;
//		List<String> oldKeyList = new ArrayList<>();
//		for (StaffFiles staffFiles : staffFilesList){
//			oldKeyList.add(staffFiles.getCompanyNameId() + "-" + staffFiles.getDocumentNumber());
//		}
		for(Object readExcel : readExcels){
			TbImportantMattersExcelBean rectificationSituationExcelBean=(TbImportantMattersExcelBean)readExcel;
			int index = readExcels.indexOf(readExcel);
			//完成情况
			if(!codeMap.get("decisionMeetTypeRef").contains(rectificationSituationExcelBean.getDecisionMeetType())){
				msg.append(ImportExcelUtil.msgBuilder("0",String.valueOf(index + 3),"决策会议类型", rectificationSituationExcelBean.getDecisionMeetType(), OBJECT_MSG));
			}
			if(!codeMap.get("matterTypeRef").contains(rectificationSituationExcelBean.getMatterType())){
				msg.append(ImportExcelUtil.msgBuilder("0",String.valueOf(index + 3),"事项类别", rectificationSituationExcelBean.getMatterType(), OBJECT_MSG));
			}
			if(!(rectificationSituationExcelBean.getIsFrontStr().equals("是")||rectificationSituationExcelBean.getIsFrontStr().equals("否"))){
				msg.append(ImportExcelUtil.msgBuilder("0",String.valueOf(index + 3),"是否党委会前置研究", rectificationSituationExcelBean.getIsFrontStr(), OBJECT_MSG));
			}
			count++;
			int value = count * 100 / size;
			CacheUtil.put("progress","progress",progressId, value);
			log.info("校验进度："+value);
		}
		//...
		//统一放置报错
		if (msg.toString().length()>0) {
			return new ImportResult(false, msg.toString());
		}
		if (msg.toString().length()>0) {
			return new ImportResult(false, msg.toString());
		}
		return importResult;
	}


}
